//用户设置
'use strict';
import React,{Component} from 'react';
import Header from '../common/header';
import styles from '../style/styles';
import Icon from 'react-native-vector-icons';
import AccountManager from './userManager';
import * as URLConstant from '../constant/URLconstant'
import Util from '../common/util'
import Toast from '../common/ToastUtil'

import {
    View,
    Text,
    TextInput,
    TouchableOpacity
} from 'react-native';
export default class userSetting extends Component{
    // 构造
      constructor(props) {
        super(props);
        // 初始状态
          this.successPost=this.successPost.bind(this)
        this.state = {
            sexList: [ '男', '女'],
            sexSelect:'',//性别
            nickname:'',//昵称
            realname:'',//真实姓名
            user_id:props.user_id
        };
      }

    componentDidMount() {
        let that= this;
        that.setState({
            sexSelect:that.props.para,
            nickname:that.props.para,
            realname:that.props.para
        })
    }

    sexSelect(sex){
        let that= this;
        that.setState({
            sexSelect:sex+1
        })
    }

    toBack(){
        let that = this;
        that.props.navigator.pop()
    }

    //确定选择的用户信息
    setUserInfo(type){
        let sexSelect = this.state.sexSelect;
        let nickname = this.state.nickname;
        let user_id=this.state.user_id
        let realname = this.state.realname;
        let url=URLConstant.UPDATAINFOMATION_URL;
        let datakey=[];
        if(type==0){
            datakey['real_name']=nickname
            datakey['user_id']=user_id
        }else if(type==1){
            datakey['gender']=sexSelect
            datakey['user_id']=user_id
        }else if(type==2){
            this.props.onPress(type,realname);
        }
        Util.post(url,datakey,this.successPost,this.errorPost)
       // this.toBack();
    }

    successPost(datas){
        //LOG(datas)
        let that=this;
        if(datas.status==0){
            Toast.show(datas.msg);
            that.props.Refresh();
            that.props.navigator.pop();
        }
    }

    errorPost(err){
        LOG(err)
    }

    showWhat(){
        let that = this;
        let type = that.props.type;
        //type=  1:昵称  2:性别  3:实名认证
        if(type==1){
            return(
                <View style={{flex:1,backgroundColor:'#f0f0f0'}}>
                    <View style={styles.nickname_wrap}>
                        <TextInput
                            style={styles.nickname_input}
                            placeholder={this.state.nickname}
                            underlineColorAndroid={'transparent'}
                            clearButtonMode={'while-editing'}
                            onChangeText={(text)=>this.setState({nickname:text})}
                        />
                    </View>
                    <Text style={styles.tips_txt}>4-20个字符,可由中英文丶数字丶"_"丶"-"组成</Text>
                    <View style={styles.confirm_btn_wrap}>
                        <TouchableOpacity style={styles.confirm_btn} onPress={()=>this.setUserInfo(0)}>
                            <Text style={styles.confirm_btn_txt}>确定</Text>
                        </TouchableOpacity>
                    </View>
                </View>
            )
        }else if(type==2){
            let rows;
            let sexList=that.state.sexList;
            let list=[];
            for(let i=0;i<2;i++){
                if(that.state.sexSelect==i+1){
                    rows=(
                        <TouchableOpacity key={i} style={styles.sex_wrap} onPress={()=>this.sexSelect(i)}>
                            <Icon name={'md-checkmark-circle'} size={25} color="#f00" />
                            <Text style={styles.sex_txt}>{sexList[i]}</Text>
                        </TouchableOpacity>
                    )
                }else{
                    rows=(
                        <TouchableOpacity key={i} style={styles.sex_wrap} onPress={()=>this.sexSelect(i)}>
                            <Icon name={'ios-radio-button-off-outline'} size={25} color="#666" />
                            <Text style={styles.sex_txt}>{sexList[i]}</Text>
                        </TouchableOpacity>
                    )
                }
                list.push(rows);
            }
            return(
                <View style={{flex:1,backgroundColor:'#f0f0f0'}}>
                    {list}
                    <View style={styles.confirm_btn_wrap}>
                        <TouchableOpacity style={styles.confirm_btn} onPress={()=>this.setUserInfo(1)}>
                            <Text style={styles.confirm_btn_txt}>确定</Text>
                        </TouchableOpacity>
                    </View>
                </View>
            );
        }else if(type==3){
            return(
                <View style={{flex:1,backgroundColor:'#f0f0f0'}}>
                    <View style={styles.nickname_wrap}>
                        <TextInput
                            style={styles.nickname_input}
                            placeholder={this.state.realname}
                            underlineColorAndroid={'transparent'}
                            clearButtonMode={'while-editing'}
                            onChangeText={(text)=>this.setState({realname:text})}
                        />
                    </View>
                    <View style={[styles.nickname_wrap,styles.nickname_border]}>
                        <TextInput
                            style={styles.nickname_input}
                            placeholder="请输入身份证号"
                            underlineColorAndroid={'transparent'}
                            clearButtonMode={'while-editing'}
                        />
                    </View>
                    <View style={styles.confirm_btn_wrap}>
                        <TouchableOpacity style={styles.confirm_btn} onPress={()=>this.setUserInfo(2)}>
                            <Text style={styles.confirm_btn_txt}>确定</Text>
                        </TouchableOpacity>
                    </View>
                </View>
            )
        }
    }

    render(){
        return(
            <View style={{flex:1}}>
                <Header navigator={this.props.navigator}  initObj={{ title: this.props.title,id:2}}/>
                {this.showWhat()}
            </View>
        )
    }
}